package cn.ivicar.sim.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;

import cn.com.carsmart.ws.ibatis.model.BaseExample;
import cn.com.carsmart.ws.model.PayRecord;
import cn.com.carsmart.ws.model.SimChargeRecord;
import cn.com.carsmart.ws.model.SimFlowDailyStat;
import cn.com.carsmart.ws.model.SimFlowInfo;
import cn.com.carsmart.ws.model.SimFlowMonthlyStat;
import cn.com.carsmart.ws.model.SimFlowPackageInfo;
import cn.com.carsmart.ws.model.SimInfo;
import cn.com.carsmart.ws.service.PayRecordManager;
import cn.com.carsmart.ws.service.SimChargeRecordManager;
import cn.com.carsmart.ws.service.SimFlowDailyStatManager;
import cn.com.carsmart.ws.service.SimFlowInfoManager;
import cn.com.carsmart.ws.service.SimFlowMonthlyStatManager;
import cn.com.carsmart.ws.service.SimFlowPackageInfoManager;
import cn.com.carsmart.ws.service.SimInfoManager;
import cn.ivicar.sim.domain.FlowStatQuery;
import cn.ivicar.sim.domain.SimInfoQuery;

/**
 * Created by lixiaozhuo on 17/5/14.
 */
public class SimService {

    @Inject
    private SimInfoManager simInfoManager;

    @Inject
    private SimChargeRecordManager simChargeRecordManager;

    @Inject
    private SimFlowDailyStatManager simFlowDailyStatManager;

    @Inject
    private SimFlowInfoManager simFlowInfoManager;

    @Inject
    private SimFlowMonthlyStatManager simFlowMonthlyStatManager;

    @Inject
    private SimFlowPackageInfoManager simFlowPackageInfoManager;

    @Inject
    private PayRecordManager payRecordManager;

    public void insertSimInfo(SimInfo simInfo) {
        simInfoManager.insert(simInfo);
    }

    public void updateSelectiveSimInfo(SimInfo simInfo) {
        simInfoManager.updateSelective(simInfo);
    }

    public SimInfo getSimInfoByIccid(String iccid) {
        SimInfo query = new SimInfo();
        query.setIccid(iccid);
        List<SimInfo> list = simInfoManager.listByExample(query);
        if (list == null || list.isEmpty()) {
            return null;
        }

        return list.get(0);
    }

    public int updateSimInfo(SimInfo simInfo){
        return simInfoManager.update(simInfo);
    }

    public void insertSimFlowInfo(SimFlowInfo simFlowInfo) {
        simFlowInfoManager.insert(simFlowInfo);
    }

    public SimFlowInfo getSimFlowInfoByIccid(String iccid) {
        SimFlowInfo query = new SimFlowInfo();
        query.setIccid(iccid);
        List<SimFlowInfo> list = simFlowInfoManager.listByExample(query);
        if (list == null || list.isEmpty()) {
            return null;
        }

        return list.get(0);
    }

    public int updateSimFlowInfo(SimFlowInfo simFlowInfo){
        return simFlowInfoManager.update(simFlowInfo);
    }

    public List<SimInfo> getSimInfoList(SimInfoQuery query){
        BaseExample example = simInfoManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if (StringUtils.isNotBlank(query.getUserCode())) {
            c.andPropertyEqualTo(query.getUserCode(), "userCode");
        }

        if(query.getOperator() != null){
            c.andPropertyEqualTo(query.getOperator(), "operator");
        }

        if(query.getCardStatus() != null){
            c.andPropertyEqualTo(query.getCardStatus(), "cardStatus");
        }

        if(query.getOrgId() != null){
            c.andPropertyEqualTo(query.getOrgId(), "orgId");
        }


        example.setLimitStart(query.getPage());
        example.setLimitCount(query.getPageSize());

        return simInfoManager.listByBaseExample(example);
    }

    public int getSimInfoCount(SimInfoQuery query){
        BaseExample example = simInfoManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if (StringUtils.isNotBlank(query.getUserCode())) {
            c.andPropertyEqualTo(query.getUserCode(), "userCode");
        }

        if(query.getOperator() != null){
            c.andPropertyEqualTo(query.getOperator(), "operator");
        }

        if(query.getCardStatus() != null){
            c.andPropertyEqualTo(query.getCardStatus(), "cardStatus");
        }

        if(query.getOrgId() != null){
            c.andPropertyEqualTo(query.getOrgId(), "orgId");
        }

        return simInfoManager.countByExample(example);
    }

    public List<SimFlowInfo> getFlowInfoList(SimInfoQuery query){
        BaseExample example = simFlowInfoManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        //TODO 查询条件string?Date?
        if(StringUtils.isNotBlank(query.getStartTimeFrom())){
            c.andPropertyGreaterThanOrEqualTo(query.getStartTimeFrom(), "startTime");
        }

        if(StringUtils.isNotBlank(query.getStartTimeTo())){
            c.andPropertyGreaterThanOrEqualTo(query.getStartTimeTo(), "startTime");
        }

        if(StringUtils.isNotBlank(query.getEndTimeFrom())){
            c.andPropertyGreaterThanOrEqualTo(query.getEndTimeFrom(), "endTime");
        }

        if(StringUtils.isNotBlank(query.getEndTimeTo())){
            c.andPropertyGreaterThanOrEqualTo(query.getEndTimeTo(), "endTime");
        }

        example.setLimitStart(query.getPage());
        example.setLimitCount(query.getPageSize());

        return simFlowInfoManager.listByBaseExample(example);
    }

    public int getFlowInfoCount(SimInfoQuery query){
        BaseExample example = simFlowInfoManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if(StringUtils.isNotBlank(query.getStartTimeFrom())){
            c.andPropertyGreaterThanOrEqualTo(query.getStartTimeFrom(), "startTime");
        }

        if(StringUtils.isNotBlank(query.getStartTimeTo())){
            c.andPropertyGreaterThanOrEqualTo(query.getStartTimeTo(), "startTime");
        }

        if(StringUtils.isNotBlank(query.getEndTimeFrom())){
            c.andPropertyGreaterThanOrEqualTo(query.getEndTimeFrom(), "endTime");
        }

        if(StringUtils.isNotBlank(query.getEndTimeTo())){
            c.andPropertyGreaterThanOrEqualTo(query.getEndTimeTo(), "endTime");
        }

        return simFlowInfoManager.countByExample(example);
    }

    public void insertFlowDailyStat(SimFlowDailyStat simFlowDailyStat){
        simFlowDailyStatManager.insert(simFlowDailyStat);
    }

    public int updateFlowDailyStat(SimFlowDailyStat simFlowDailyStat){
        return simFlowDailyStatManager.update(simFlowDailyStat);
    }

    public List<SimFlowDailyStat> getFlowDailyStatList(FlowStatQuery query){
        BaseExample example = simFlowDailyStatManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if(query.getStartDate() != null){
            c.andPropertyGreaterThanOrEqualTo(query.getStartDate(), "occurDate");
        }

        if(query.getEndDate() != null){
            c.andPropertyLessThanOrEqualTo(query.getEndDate(), "occurDate");
        }

        if(query.getOrgId() != null){
            c.andPropertyEqualTo(query.getOrgId(), "orgId");
        }

        example.setLimitStart(query.getPage());
        example.setLimitCount(query.getPageSize());

        return simFlowDailyStatManager.listByBaseExample(example);
    }

    public int getFlowDailyStatCount (FlowStatQuery query){
        BaseExample example = simFlowDailyStatManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if(query.getStartDate() != null){
            c.andPropertyGreaterThanOrEqualTo(query.getStartDate(), "occurDate");
        }

        if(query.getEndDate() != null){
            c.andPropertyLessThanOrEqualTo(query.getEndDate(), "occurDate");
        }

        if(query.getOrgId() != null){
            c.andPropertyEqualTo(query.getOrgId(), "orgId");
        }

        return simFlowDailyStatManager.countByExample(example);
    }

    public void insertFlowMonthlyStat(SimFlowMonthlyStat simFlowMonthlyStat){
        simFlowMonthlyStatManager.insert(simFlowMonthlyStat);
    }

    public int updateFlowMonthlyStat(SimFlowMonthlyStat simFlowMonthlyStat){
        return simFlowMonthlyStatManager.update(simFlowMonthlyStat);
    }

    public List<SimFlowMonthlyStat> getFlowMonthlyStatList(FlowStatQuery query){
        BaseExample example = simFlowMonthlyStatManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if(query.getStartDate() != null){
            c.andPropertyGreaterThanOrEqualTo(query.getStartDate(), "occurMonth");
        }

        if(query.getEndDate() != null){
            c.andPropertyLessThanOrEqualTo(query.getEndDate(), "occurMonth");
        }

        if(query.getOrgId() != null){
            //TODO 表中缺orgId
            c.andPropertyEqualTo(query.getOrgId(), "orgId");
        }

        example.setLimitStart(query.getPage());
        example.setLimitCount(query.getPageSize());

        return simFlowMonthlyStatManager.listByBaseExample(example);
    }

    public int getFlowMonthlyStatCount(FlowStatQuery query){
        BaseExample example = simFlowMonthlyStatManager.getExample();
        BaseExample.Criteria c = example.or();
        if (StringUtils.isNotBlank(query.getIccid())) {
            c.andPropertyEqualTo(query.getIccid(), "iccid");
        }

        if(query.getStartDate() != null){
            c.andPropertyGreaterThanOrEqualTo(query.getStartDate(), "occurDate");
        }

        if(query.getEndDate() != null){
            c.andPropertyLessThanOrEqualTo(query.getEndDate(), "occurDate");
        }

        if(query.getOrgId() != null){
            c.andPropertyEqualTo(query.getOrgId(), "orgId");
        }

        return simFlowMonthlyStatManager.countByExample(example);
    }

    public void insertPayRescord(PayRecord payRecord) {
        payRecordManager.insert(payRecord);
    }

    public void updatePayRescord(PayRecord payRecord) {
        payRecordManager.update(payRecord);
    }

    public void insertSimFlowPackageInfo(SimFlowPackageInfo simFlowPackageInfo) {
        simFlowPackageInfoManager.insert(simFlowPackageInfo);
    }

    //TODO 考虑该表中是否还需要在添加iccid字段，packageId是否是全局唯一的
    public SimFlowPackageInfo getSimFlowPackageInfoById(String packageId) {
        SimFlowPackageInfo query = new SimFlowPackageInfo();
        query.setFlowPackageId(packageId);
        List<SimFlowPackageInfo> list = simFlowPackageInfoManager.listByExample(query);
        if (list == null || list.isEmpty()) {
            return null;
        }

        return list.get(0);
    }
    
    public List<SimFlowPackageInfo> getSimFlowPackageInfoByOperator(Integer operator) {
        SimFlowPackageInfo query = new SimFlowPackageInfo();
        query.setOperator(operator);
        query.setIsDeleted(false);
        List<SimFlowPackageInfo> list = simFlowPackageInfoManager.listByExample(query);
        if (list == null || list.isEmpty()) {
            return null;
        }

        return list;
    }
    
    public void save(SimFlowPackageInfo simFlowPackageInfo){
    	SimFlowPackageInfo result = this.getSimFlowPackageInfoById(simFlowPackageInfo.getFlowPackageId());
    	if(result == null){
    		this.insertSimFlowPackageInfo(simFlowPackageInfo);
    	}else {
    		simFlowPackageInfo.setId(result.getId());
    		simFlowPackageInfoManager.updateSelective(simFlowPackageInfo);
		}
    }

    public void insertSimChargeRescord(SimChargeRecord simChargeRecord) {
        simChargeRecordManager.insert(simChargeRecord);
    }

    public void updateSimChargeRecord(SimChargeRecord simChargeRecord) {
        simChargeRecordManager.update(simChargeRecord);
    }

    public SimChargeRecord getChargeRecordById(long chargeId) {
        SimChargeRecord query = new SimChargeRecord();
        query.setChargeId(chargeId);
        List<SimChargeRecord> list = simChargeRecordManager.listByExample(query);
        if (list == null || list.isEmpty()) {
            return null;
        }

        return list.get(0);
    }

    public PayRecord getPayRecordById(Long payRecordId) {
        return payRecordManager.get(payRecordId);
    }
}
